<template>
  <span>{{ content }}</span>
</template>

<script>
export default {
  data () {
    return {
      content: '',
      time:0,
      dateTimeStamp:0
    }
  },
  props: {
    date: {
      type: String,
      default: new Date().toString()
    },
    dateString:{
      type: String
    }
  },
  mounted() {
    if(this.dateString){
      this.dateChange();
    }
    this.diffTime()
  },
  watch: {
    date () {
      this.diffTime()
    },
    dateString(){
      this.dateChange()
    }
  },
  methods: {
    dateChange(){
      // 2019-02-20T16:00:00
      let date = this.dateString.split('T')[0];
      let time = this.dateString.split('T')[1].split('.')[0];
      let year = parseInt(date.split('-')[0]);
      let month = parseInt(date.split('-')[1]);
      let day = parseInt(date.split('-')[2]);
      let hour = parseInt(time.split(':')[0]);
      let minute = parseInt(time.split(':')[1]);
      let second = parseInt(time.split(':')[2]);
      // 转化为时间戳
      this.time = new Date(year,month-1,day,hour,minute,second).getTime();
      // console.log(this.time)
      this.diffTime()

    },
    diffTime () {
        this.dateTimeStamp = this.time
        // 开始判断当前时间戳和现在时间戳比较，得出时间差
        let minute = 1000 * 60;
        let hour = minute * 60;
        let day = hour * 24;
        let halfamonth = day * 15;
        let month = day * 30;
        let now = new Date().getTime();
        let diffValue = now - this.dateTimeStamp;
        // console.log(diffValue)
        if(diffValue < 0){
          return;
        }
        let monthC =diffValue/month;
        let weekC =diffValue/(7*day);
        let dayC =diffValue/day;
        let hourC =diffValue/hour;
        let minC =diffValue/minute;
        // console.log(monthC,weekC,dayC,hourC,minC)
        if(monthC>=1){
          this.content="" + parseInt(monthC) + "月前";
        }
        else if(weekC>=1){
          this.content="" + parseInt(weekC) + "周前";
        }
        else if(dayC>=1){
          this.content=""+ parseInt(dayC) +"天前";
        }
        else if(hourC>=1){
          this.content=""+ parseInt(hourC) +"小时前";
        }
        else if(minC>=1){
          this.content=""+ parseInt(minC) +"分钟前";
        }else this.content="刚刚";
    }
  }
}
</script>

<style>

</style>

